<template>
  <div class="mainsu">
    <div class="main_spac1">
      <div class="main_white padding10 paddingBottom20">
        <div class="mainor_form">
          <div class="mainor_te">
            <el-button class="mainor_tea" @click="pldeliver"
              >批量发货</el-button
            >
          </div>
          <el-form
            :inline="true"
            :model="form"
            class="demo-form-inline"
            ref="userListFrom"
            label-width="70px"
          >
            <el-form-item label="配送日期" prop="send_time">
              <el-date-picker
                v-model="form.send_time"
                type="date"
                class="wid220"
                :editable="false"
                placeholder="选择配送日期"
                value-format="yyyy-MM-dd"
                :clearable="false"         
              />
            </el-form-item>
            <el-form-item label="配送时间" prop="send_time_id">
              <el-select
                v-model.trim="form.send_time_id"
                placeholder="配送时间"
                class="wid220"
              >
                <el-option label="所有" value=""></el-option>
                <el-option
                  v-for="(send_time_list, index) in list.send_time_list"
                  :key="index"
                  :label="send_time_list"
                  :value="parseInt(index)"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="配送路线" prop="line_id">
              <el-select
                v-model.trim="form.line_id"
                placeholder="配送路线"
                class="wid220"
              >
                <el-option label="所有" value=""></el-option>
                <el-option
                  v-for="(sort_status, index) in line_info"
                  :key="index"
                  :label="sort_status"
                  :value="index"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="分拣状态" prop="status">
              <el-select
                v-model.trim="form.status"
                placeholder="分拣状态"
                class="wid220"
              >
                <el-option label="所有" value=""></el-option>
                <el-option
                  v-for="(sort_status, index) in list.sort_status"
                  :key="index"
                  :label="sort_status"
                  :value="index + 1"
                ></el-option>
              </el-select>
            </el-form-item>
            <div class="more el-form-item" @click="more">
              <span>更多</span>
              <i :class="[{ on: moredata }, ' el-icon-arrow-down']"></i>
            </div>
            <div class="clearfix"></div>
            <el-collapse-transition>
              <div v-if="moredata">
                <div>
                  <el-form-item label="用户" prop="uid">
                    <change-user-input
                      ref="changeUserInput"
                      @sendValue="getUser"
                      @clear="clearUid"
                    ></change-user-input>
                  </el-form-item>
                  <el-form-item label="订单" prop="order_sn">
                    <el-input
                      v-model.trim="form.order_sn"
                      placeholder="订单"
                      clearable
                      class="wid220"
                    ></el-input>
                  </el-form-item>
                  <!-- 新增发货状态 待确认绑定值 -->
                  <el-form-item label="发货状态" prop="order_status">
                    <el-select
                      v-model.trim="form.order_status"
                      placeholder="发货状态"
                      class="wid220"
                    >
                      <el-option label="所有" value=""></el-option>
                      <el-option label="已发货" value="2"></el-option>
                      <el-option label="未发货" value="1"></el-option>
                      <!-- <el-option
                      v-for="(item, index) in list.order_status"
                      :key="index"
                      :label="item"
                      :value="parseInt(index) + 1"
                    ></el-option> -->
                    </el-select>
                  </el-form-item>
                </div>
              </div>
            </el-collapse-transition>
            <el-form-item>
              <el-button
                class="reset marginLeft10"
                type="primary"
                @click="reset"
                >重置</el-button
              >
              <el-button
                type="primary"
                @click="getArticleList"
                :loading="listLoading"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </div>
        <div class="main_num boxdq marginTop10">
          <div class="main_num_l box">
            <p>
              订单数量：
              <em>{{ list.all }}</em>
            </p>
            <p>
              未发订单：
              <em>{{ list.all_un }}</em>
            </p>
            <p>
              已发订单：
              <em>{{ list.all_ed }}</em>
            </p>
            <div class="mainsu_te marginRight5">
              <el-tooltip class="item" effect="dark" placement="top">
                <div slot="content">注释：已完成分拣，但未发货的订单</div>
                <svg
                  aria-hidden="true"
                  class="navbar_svg"
                  width="16"
                  height="16"
                >
                  <use xlink:href="#icon-summary_question" />
                </svg>
              </el-tooltip>
            </div>
            <p>
              可发订单：
              <em>{{ list.all_ok }}</em>
            </p>
            <p class="box" style="width: 400px; align-items: center">
              发货进度：
              <el-progress
                :text-inside="true"
                :stroke-width="14"
                :percentage="fenjian_percent"
                status="success"
                color="#E47C02"
                style="width: 60%"
              ></el-progress>
            </p>
          </div>
        </div>
        <div class="main_table marginTop10" style="min-height: 550px">
          <el-table
            ref="multipleTable"
            v-loading="listLoading"
            :data="list.list.data"
            element-loading-text="加载中"
            fit
            border
            highlight-current-row
            @selection-change="handleSelectionChange"
            :header-cell-class-name="this.$verticaline"
          >
            <el-table-column type="selection" :selectable="selectInit" />
            <el-table-column label="订单号" width="180" prop="order_sn" />
            <el-table-column label="用户名称">
              <template slot-scope="scope"
                >{{ scope.row.member ? scope.row.member.nickname : "" }}
                <span v-if="scope.row.zid_info"
                  >【{{ scope.row.zid_info.nickname }}】</span
                ></template
              >
            </el-table-column>
            <el-table-column label="配送时间" prop="delivery_time">
              <template slot-scope="scope">{{
                scope.row.delivery_time
                  ? parseTime(scope.row.delivery_time, "{y}-{m}-{d}")
                  : ""
              }}</template>
            </el-table-column>
            <el-table-column label="分拣状态" prop="status">
              <template slot-scope="scope"
                ><span :class="'order' + scope.row.order_list_status">
                  {{ list.sort_status[scope.row.order_list_status] }}
                </span></template
              >
            </el-table-column>
            <el-table-column label="发货状态" prop="status">
              <template slot-scope="scope"
                ><span
                  :class="
                    scope.row.order_status == 2 || scope.row.order_status == 3
                      ? 'success'
                      : 'unpaid'
                  "
                >
                  {{
                    scope.row.order_status == 2 || scope.row.order_status == 3
                      ? "已发货"
                      : "未发货"
                  }}</span
                ></template
              >
            </el-table-column>
            <el-table-column label="配送路线" prop="line_id">
              <template slot-scope="scope"
                ><span>
                  {{ line_info[scope.row.line_id] }}
                </span></template
              >
            </el-table-column>
            <el-table-column
              label="管理操作"
              align="left"
              fixed="right"
              width="200"
              class-name="main_hei"
            >
              <template slot-scope="scope">
                <el-link
                  type="success"
                  @click="(id = scope.row.id), sendOrder_info(scope)"
                  >查看</el-link
                >
                <el-link
                  type="warning"
                  @click="deliver(scope.$index, scope.row.id)"
                  v-if="
                    scope.row.order_status == 1 &&
                    scope.row.order_list_status == 2
                  "
                  >发货</el-link
                >
                <!-- <el-link>打印</el-link> -->
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="main_page boxdq">
          <div></div>
          <!-- 分页 -->
          <div class="main_page_r">
            <el-pagination
              :current-page="current.page"
              :page-sizes="[50, 100, 200]"
              :page-size="limit"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totalCount"
              background
              @size-change="changeLimit"
              @current-change="changePage"
            />
          </div>
        </div>

        <!-- 批量发货 -->
        <el-dialog
          :title="'已选择' + selectArr.length + '个订单，发货出库中！'"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
          @close="close"
          :visible.sync="addedFormVisible"
          class="main_dialog dialog450"
        >
          <el-progress
            :text-inside="true"
            :stroke-width="15"
            :percentage="percent"
            status="success"
            color="#E47C02"
            style="width: 100%"
          ></el-progress>
          <div class="progress_list">
            <template v-for="(percent_list, index) in reverseSum">
              <template v-if="index < 5">
                <p v-if="percent_list.type == 1" :key="index">
                  订单id:{{ percent_list.order_id }}，发货已成功
                </p>
                <p v-else class="progress_list_red" :key="index">
                  订单id:{{ percent_list.order_id }}，发货失败
                </p>
              </template>
            </template>
            <p v-if="percent == 100" class="progress_list_su">
              发货已经完成请关闭弹框!
            </p>
          </div>
        </el-dialog>
        <!-- 查看 -->
        <el-dialog
          title="查看"
          :visible.sync="seeFormVisible"
          class="main_dialog dialog840"
        >
          <div class="main_xq">
            <ul class="boxha">
              <li>
                订单号：<span>{{ info.info.order_sn }}</span>
              </li>
              <li>
                用户名称：<span>{{ info.info.nickname }}</span
                ><span v-if="info.info.zid_info"
                  >【{{ info.info.zid_info.nickname }}】</span
                >
              </li>
              <li>
                收货人：<span>{{ info.info.contact }}</span>
              </li>
              <li>
                收货手机：<span>{{ info.info.mobile }}</span>
              </li>
              <li class="main_w100">
                收货地址：<span>{{ info.info.address }}</span>
              </li>
            </ul>
          </div>
          <div class="main_table marginTop20">
            <el-table
              ref="multipleTable1"
              v-loading="listLoading"
              :data="info.info.order_list"
              element-loading-text="加载中"
              fit
              highlight-current-row
            >
              <el-table-column label="序号">
                <template slot-scope="scope">{{ scope.$index + 1 }}</template>
              </el-table-column>
              <el-table-column
                label="商品名称"
                prop="userNickname"
                class-name="main_hei"
                width="180"
              >
                <template slot-scope="scope">
                  <span
                    v-if="scope.row.out_status == 1 ? true : false"
                    style="color: #e6a23c"
                  >
                    【缺】
                  </span>
                  <span
                    v-if="scope.row.modify == 1 ? true : false"
                    style="color: #e6a23c"
                  >
                    【更】 </span
                  >{{ scope.row.name }}【{{
                    scope.row.goods_spu.spu_name.title
                  }}】</template
                >
              </el-table-column>
              <el-table-column
                label="SKU编码"
                prop="goods_spu.goods_spu_code"
                class-name="main_hei"
              />
              <el-table-column
                label="下单量"
                prop="order_num"
                class-name="main_hei"
              >
                <template slot-scope="scope">
                  <span
                    :style="{
                      color: scope.row.out_status == 1 ? '#e6a23c' : '',
                    }"
                  >
                    {{ scope.row.order_num + scope.row.unit }}
                  </span></template
                >
              </el-table-column>
              <el-table-column
                label="分拣量"
                prop="sort_num"
                class-name="main_hei"
              >
                <template slot-scope="scope">
                  <span
                    :style="{
                      color: scope.row.out_status == 1 ? 'red' : '',
                    }"
                    >{{
                      scope.row.sort_num +
                      (scope.row.transfer_unit
                        ? scope.row.transfer_unit
                        : scope.row.unit)
                    }}</span
                  ></template
                >
              </el-table-column>
              <el-table-column
                label="当前库存"
                prop="userNickname"
                class-name="main_hei"
              >
                <template slot-scope="scope"
                  ><span v-if="is_delivery">
                    {{
                      scope.row.goods_spu.inventory
                        ? scope.row.goods_spu.inventory.num
                        : "0.00"
                    }}{{ scope.row.unit }}</span
                  >
                  <span v-else> / </span>
                </template>
              </el-table-column>
              <el-table-column label="分拣状态" prop="status">
                <template slot-scope="scope"
                  ><span :class="[scope.row.status == 1 ? 'sort1' : 'sort0']">
                    {{ info.status[scope.row.status] }}</span
                  ></template
                >
              </el-table-column>
            </el-table>
          </div>
          <div slot="footer" class="dialog-footer">
            <el-button
              type="primary"
              @click="seeupmine"
              v-repeatClick
              v-if="
                info.info.order_status == 1 && info.info.order_list_status == 2
              "
              >发 货</el-button
            >
          </div>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import {
  sendOrder_index,
  sendOrder_info,
  sendOrder_bd,
  sendOrder_batchDispatchGoods,
} from "@/api/warehouse/warehouseDelivery";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import { parseTime } from "@/utils/index";

export default {
  computed: {
    ...mapGetters(["avatar"]),
    reverseSum() {
      return this.percent_list.reverse();
    },

    // 分拣经度进度
    fenjian_percent() {
      let percent = 0;
      let total = this.totalCount;

      this.list.list.data.map((e) => {
        if (e.order_status == 2 || e.order_status == 3) {
          percent += 1;
        }
      });
      if (total) {
        percent = parseFloat((percent / total) * 100).toFixed(2);
      }
      return parseFloat(percent);
    },
  },
  data() {
    return {
      // 时间
      parseTime,
      // 分页
      current: {
        page: 1,
        per_page: 50,
      },
      limit: 50,
      totalCount: 1,
      // 更多
      moredata: false,
      // 列表数据
      list: {
        list: {
          data: [],
        },
      },
      line_info: {},
      info: {
        info: {
          order_sn: "",
          contact: "",
          mobile: "",
          address: "",
          company_name: {
            company_name: "",
          },
          order_list: [],
        },
        order_status: ["未分拣"],
        status: ["未分拣"],
      },
      id: "",
      seeFormVisible: false,
      listLoading: false,
      // 搜索
      form: {
        send_time: parseTime(new Date(), "{y}-{m}-{d}"),
        send_time_id: null,
        status: null,
        uid: "",
        order_sn: "",
        order_status: null,
        line_id: "",
      },
      // 是否发货
      is_delivery: true,
      // 批量发货
      percent_gs: 0,
      percent: 0,
      percent_list: [],
      addedFormVisible: false,
      // 全选
      selectArr: [],
    };
  },
  mounted() {
    window.Echo.channel("batchGoods." + this.avatar.company_id).listen(
      "BacthGoodsEvent",
      (e) => {
        console.log(e);
        this.percent_gs = parseInt(this.percent_gs) + 1;
        if ((this.percent_gs / this.selectArr.length) * 100 >= 100) {
          this.percent = 100;
          return;
        }
        this.percent =
          this.percent_gs / this.selectArr.length
            ? parseInt((this.percent_gs / this.selectArr.length) * 100)
            : 0;
        if (e.notice.msg == "ok") {
          this.percent_list.push({
            num: this.percent,
            type: 1,
            order_id: e.notice.order_id,
          });
        } else {
          this.percent_list.push({
            num: this.percent,
            type: 2,
            order_id: e.notice.order_id,
          });
        }
      }
    );
  },
  created() {
    // 列表
    this.sendOrder_index();
  },
  methods: {
    //获取用户信息
    getUser(data) {
      let uid = data.info.user_id;
      this.form.uid = uid;
      this.sendOrder_index();
    },
    // 清除用户的uid
    clearUid() {
      this.form.uid = "";
    },
    // 关闭清空表单
    close() {
      this.percent = 0;
      this.percent_gs = 0;
      this.percent_list = [];
      this.sendOrder_index();
      // this.$refs["userListFrom1"].clearValidate();
    },
    // 监听全选
    handleSelectionChange(val) {
      this.selectArr = [];
      val.map((item) => {
        this.selectArr.push({ id: item.id });
      });
    },
    // 监听绑定全选不可勾选
    selectInit(row, index) {
      if (row.order_status == 1 && row.order_list_status == 2) {
        return true;
      } else {
        return false;
      }
    },
    // 查看发货
    sendOrder_info(scope) {
      if (scope.row.order_status == 2 || scope.row.order_status == 3) {
        this.is_delivery = false;
      } else {
        this.is_delivery = true;
      }

      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, { id: this.id })) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { id: this.id }
      );
      sendOrder_info(data).then((res) => {
        this.seeFormVisible = true;
        if (res.errcode == 0) {
          this.info = res.data;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    seeupmine() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, { id: this.id })) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { id: this.id }
      );
      this.$confirm("确定发货吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          sendOrder_bd(data).then((res) => {
            this.seeFormVisible = false;
            if (res.errcode == 0) {
              this.$message({
                message: "发货成功",
                type: "success",
              });
              this.sendOrder_index();
            } else {
              this.$message.error(res.errmsg);
            }
          });
        })
        .catch(() => {});
    },
    // 批量发货
    setupmine() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { ids: JSON.stringify(this.selectArr) }
      );
      sendOrder_batchDispatchGoods(data).then((res) => {
        this.addedFormVisible = true;
      });
    },
    pldeliver() {
      if (this.selectArr.length != 0) {
        this.setupmine();
      } else {
        this.$message.error("请勾选订单");
      }
    },
    // 发货
    deliver(index, id) {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, { id: id })) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { id: id }
      );
      this.$confirm("确定发货吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          sendOrder_bd(data).then((res) => {
            if (res.errcode == 0) {
              this.$message({
                message: "发货成功",
                type: "success",
              });
              this.sendOrder_index();
            } else {
              this.$message.error(res.errmsg);
            }
          });
        })
        .catch(() => {});
    },
    // 搜索
    getArticleList() {
      this.current.page = 1;
      this.sendOrder_index();
    },
    // 重置
    reset() {
      this.$refs["userListFrom"].resetFields();
      this.$refs["changeUserInput"].name = "";
      this.sendOrder_index();
    },
    // 更多
    more() {
      this.moredata = !this.moredata;
    },
    // 分页
    changeLimit(pageSize) {
      this.current.per_page = pageSize;
      this.sendOrder_index();
    },
    changePage(page) {
      this.current.page = page;
      this.sendOrder_index();
    },
    // 列表
    sendOrder_index() {
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, this.list_form(), this.current)
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.list_form(),
        this.current
      );
      sendOrder_index(data).then((res) => {
        this.listLoading = false;
        if (res.errcode == 0) {
          this.list = res.data;
          this.totalCount = res.data.list.total;
          this.line_info = res.data.line_info;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    list_form() {
      let form = JSON.parse(JSON.stringify(this.form));

      return form;
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.mainsu {
  .mainor_form form {
    width: calc(100% - 120px);
  }
  .main_num {
    .mainsu_te {
      position: relative;
      top: -1px;
    }
  }
}
/deep/.el-progress-bar__innerText {
  transform: translateY(-1px);
}
.main_dialog {
  .main_table {
    /deep/.el-table__body-wrapper {
      overflow-y: auto;
      height: 400px;
    }
  }
  .main_xq ul li {
    width: 33.33%;
    margin-top: 5px;
    margin-bottom: 5px;
  }
  .progress_list {
    margin-top: 10px;
    p {
      height: 24px;
      line-height: 24px;
    }
    .progress_list_red {
      color: red;
    }
    .progress_list_su {
      color: #03a98e;
    }
  }
}

// 支付状态
.success {
  color: $order_color3;
}
.unpaid {
  color: $colour_red;
}
// 订单状态
.order1 {
  color: $order_color1;
}
.order2 {
  color: $order_color2;
}
.order3 {
  color: $order_color3;
}
.order4 {
  color: $order_color4;
}

// 分拣状态
.sort1 {
  color: #03a98e;
}
.sort0 {
  color: red;
}
</style>
